/*JavaScript Document*/
var js_debug = true;
jQuery(document).ready(function () {
	/*********************
		Users checks
	*********************/
	jQuery('#email').focus();
	/*EOF user checks*/

	setTimeout("hideme('error_m','slow');",20000);	
	
	jQuery('a[rel*=facebox]').facebox();
	
	//jQuery.facebox('Sorry, for security reasons<br/>you have been looged out from the system<br/>', 'timeout_m');
	
  /*This will set backgroud color for a form textbox, highlightings it.
   *This will work on bodyOnLoad()
   *onload effect
   **/
  
	// Textbox			   
	$('input[type="text"]').each(function (i) {										   	
		$(this).addClass('onblur_f');
 	}); 
	// Password			   
	$('input[type="password"]').each(function (i) {		
		$(this).addClass('onblur_f');
 	}); 
	// Textarea			   
	$('textarea').each(function (i) {
		$(this).addClass('onblur_f');
 	});
	// select			   
	$('select').each(function (i) {
		$(this).addClass('onblur_f');
 	});
	
	//onfocus effect
	// Textbox			   
	$('input[type="text"]').each(function (i) {										   	
		//$(this).removeClass('focus_f');									 
		$(this).focus(function () {
		  $(this).addClass('focus_f');
		   $(this).removeClass('onblur_f');
    	});		
		$(this).blur(function () { 			
		   $(this).removeClass('focus_f');
		    $(this).addClass('onblur_f');
    	});
		
 	}); 
	// Password			   
	$('input[type="password"]').each(function (i) {		
		//$(this).removeClass('focus_f');									 
		$(this).focus(function () {
		  $(this).addClass('focus_f');
		  $(this).removeClass('onblur_f');
    	});		
		$(this).blur(function () { 			
		   $(this).removeClass('focus_f');
		   $(this).addClass('onblur_f');
    	});
 	});
	// Textarea			   
	$('textarea').each(function (i) {
		//$(this).removeClass('focus_f');									 
		$(this).focus(function () {
		  $(this).addClass('focus_f');
		  $(this).removeClass('onblur_f');
    	});		
		$(this).blur(function () { 			
		   $(this).removeClass('focus_f');
		   $(this).addClass('onblur_f');
    	});
 	});
	//select
	$('select').each(function (i) {
		//$(this).removeClass('focus_f');									 
		$(this).focus(function () {
		  $(this).addClass('focus_f');
		  $(this).removeClass('onblur_f');
    	});		
		$(this).blur(function () { 			
		   $(this).removeClass('focus_f');
		   $(this).addClass('onblur_f');
    	});
 	});
});/*EOF jQuery*/

/*********************
	Load external JavaScript
*********************/	
function loadscript(url, callback){
	var script = document.createElement("script")
    script.type = "text/javascript";
    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function(){
            callback();
        };
    }
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}
/*********************
 	Hide Me - Hides a particular DOM element
*********************/	
function hideme(id,s){
	var c = (s != 'slow' || s != 'fast')?'slow':s;
	jQuery('#'+id).hide(c);
}
/*********************
 	Show Me - Shows/ Displays a particular DOM element
*********************/	
function showme(id,s){
	alert(id);
	var c = (s != 'slow' || s != 'fast')?'slow':s;
	jQuery('#'+id).show(c);
}
/*********************
	slideme - slide toggle
*********************/
function slideme(id,c,s){
	var obj = (c == 'cl')? '.'+id:'#'+id;
	var s = (s == 'f')?'fast':'slow';
	jQuery(obj).slideToggle(s);
}
/*********************
	loadme
*********************/
function loadme(def)
{
	var str = '';
	var sometext = (def != '' && def != undefined)?def:"loading page.....";

	str += '<div id="loadme" style="position:absolute;top:0px;left:0px;right:5px;background-color:#FFFF84;width:185px;height:22px;z-index:100; display:block;font-family: Lucida Grande, Verdana, Sans-serif;">';
	str += '<div align="center" style="padding:3px;width:100%;color:#444444;font-size:12px;font-weight:bold;">'+sometext+'<\/div>';
	str += '<\/div>';
	
	if(!document.getElementById('loadme'))
		document.write(str);
		
	document.getElementById('loadme').focus();
}

function is_loaded(){
	if(document.getElementById('footer')){
		document.getElementById('loadme').style.display='none';
	}else{
		loadme();
		setTimeout("is_loaded()",900);
	}
}
is_loaded();//call me always
/*********************
	redirect page - page redirects supporting all browsers
	@param = string
*********************/
function redirect_page(url){/*alert('->'+url);*/
	if(url != ""){
		var browser_type=navigator.appName
		var browser_version=parseInt(navigator.appVersion)
		if (browser_type=="Netscape"&&browser_version>=4){
			//if NS 4+	
			window.location.replace(url);
		}else if (browser_type=="Microsoft Internet Explorer"&&browser_version>=4){
			//if IE 4+	
			window.location.replace(url);
		}else{
			//Default goto page (NOT NS 4+ and NOT IE 4+)	
			window.location=url;	
		}
	}
}

/*********************
 *@param = string form id
 *@param = string form name
 *@param = string action url
 *@param = string form mode
*********************/
var bk_frm_id = 0;
var bk_frm_mode = '';
var bk_frm_action = '';
var bk_frm_frm = '';
/*********************
	do submit- page will submit the form using JS	
*********************/
function do_submit()
{	var obj = document.getElementById(bk_frm_frm);		
	if(obj != "" || obj != "undefined"){
		obj.action = bk_frm_action;
		if(isNaN(bk_frm_id) == false){
			obj.frm_id.value = bk_frm_id;
		}
    if(bk_frm_mode != ''){
			obj.frm_mode.value = bk_frm_mode;
		}
		obj.submit();
	}
}

/*set error or congrat msg*/
var cong_msg ='<div id="admin_msg"><div class="success"><span>Congratulations!</span></div></div>';
var err_msg ='<div id="admin_msg"><div class="error"><span>Error(s)!</span></div></div>';

/*********************
	Call Ajax - functions
*********************/
//ajax default messages
var ajx_msg 		= "Please wait, processing your request...";
var ajx_err 		= "Sorry, the service your trying to reach is currently not responding.<br\/>Kindly try again later.";
//ajax default settings
var ajx_type		= 'post';
var ajx_url			= "";
var ajx_formdata	= {};
var ajx_timeout		= 3000;
var ajx_data_type	= "";//json or ''
var ajx_cache		= false;
var ajx_data 		= '';
//ajax default container and settings
var ajx_container 	= '';
var ajx_return 		= false;
var ajx_handle_func	= '';
var ajx_loader = '<img src="'+base_url+'/js/facebox/loading.gif'+'" \/>';
function call_ajax()
{	
	var str = '';
	jQuery.ajax({
		type: ajx_type,
		url: ajx_url,
		data: ajx_formdata,
		dataType: ajx_data_type,
		timeout: ajx_timeout,
		cache: ajx_cache,
		beforeSend: function(xhr) {  
			jQuery.facebox(ajx_msg+ajx_loader);
		}, 
		error: function (XMLHttpRequest, textStatus, errorThrown) {			
			if(js_debug == true){
				str += 'XMLHttpRequest : ';
				str += XMLHttpRequest+'<br\/>';
				str += 'textStatus : ';
				str += textStatus+'<br\/>';
				str += 'errorThrown : ';
				str += errorThrown+'<br\/>';				
				jQuery.facebox(str);
			}else{
				jQuery.facebox(ajx_err);
			}
		},
		success: function (data) {
			
			ajx_data = data;
      if(ajx_data_type == 'json'){
        
        if(ajx_data.suc == 1){
          var newmsg = cong_msg+ajx_data.msg;   
          jQuery.facebox(newmsg); 
        }else if(ajx_data.err == 1){
          var newmsg = err_msg+ajx_data.msg;   
          jQuery.facebox(newmsg); 
        }
        
        if(ajx_data.act != '' && ajx_data.act != 'undefined'){
          var red_url = decode(ajx_data.act);
          redirect_page(red_url);
        }
      }else{
        if(ajx_data_type == 'update'){
          $('#'+ajx_container).html('');
          $('#'+ajx_container).html(ajx_data);
        }
        jQuery(document).trigger('close.facebox');
      }
		},
		complete: function (XMLHttpRequest, textStatus){
			
		}
	});	
}

function run_ajax()
{	
	var str = '';
	jQuery.ajax({
		type: ajx_type,
		url: ajx_url,
		data: ajx_formdata,
		dataType: ajx_data_type,
		timeout: ajx_timeout,
		cache: ajx_cache,
		beforeSend: function(xhr) {}, 
		error: function (XMLHttpRequest, textStatus, errorThrown) {},
		success: function (data) {},
		complete: function (XMLHttpRequest, textStatus){}
	});	
}

/*********************
	Decode - data
*********************/
function decode(str) {  
     return unescape(str.replace(/\+/g, " "));
}
/*********************
	Call Ajax - functions
*********************/
function ajx_handle_dataset()
{
	//any functions that needs to be called after the ajax request
  eval(ajx_handle_func);
}

/*********************
	Set Country
*********************/
function set_country(){  
  ajx_container 	= 'admin_msg';
	ajx_url 		= jQuery("#frmchncountry").attr("action");	
	ajx_formdata 	= jQuery("#frmchncountry").serialize();
  ajx_formdata += "&cur_url="+cur_url;
  ajx_data_type = 'json';
  call_ajax();
}
/*********************
	Adimn Login
*********************/
function admin_login()
{		
	//jQuery("#frm_login input[name=submit_login]").val("Processing....");
	ajx_container 	= 'admin_msg';
	ajx_url 		= jQuery("#frmlogin").attr("action");	
	ajx_formdata 	= jQuery("#frmlogin").serialize();
	/*custom call*/
  var str = '';
	jQuery.ajax({
		type: ajx_type,
		url: ajx_url,
		data: ajx_formdata,
		dataType: ajx_data_type,
		timeout: ajx_timeout,
		cache: ajx_cache,
		beforeSend: function(xhr) {  
			jQuery.facebox(ajx_msg);
		}, 
		error: function (XMLHttpRequest, textStatus, errorThrown) {			
			if(js_debug == true){
				str += 'XMLHttpRequest : ';
				str += XMLHttpRequest+'<br\/>';
				str += 'textStatus : ';
				str += textStatus+'<br\/>';
				str += 'errorThrown : ';
				str += errorThrown+'<br\/>';				
				jQuery.facebox(str);
			}else{
				jQuery.facebox(ajx_err);
			}
		},
		success: function (data) {
			jQuery(document).trigger('close.facebox');		
			ajx_data = data;			      
      var chk_cong = /Congratulations/ig.test(ajx_data);      
      document.getElementById(ajx_container).innerHTML = ajx_data;
      if(chk_cong == true){
        setTimeout("redirect_page( site_url + '/dashboard');", 2000);
      }
		}
	});
	return false;
}

/*********************
	C Num - validate
  This will only allow numbers to be pressed on keyPress event 
  
  Example : onKeyPress="return(c_num(event));"  
  @access public
  @param  event object	 
  @return void
*********************/
function c_num(e) {
  var strCheck = '0123456789\b\t	 ';
  var whichCode = (window.event) ? e.which : e.keyCode;
  if (whichCode == 0 || whichCode == 13)
  {
    return true ;		
  }
  key = String.fromCharCode(whichCode);  // Get key value from key code
  if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
}


$(function() {
  $( ".datepicker" ).datepicker({
    showOn: "both",
    dateFormat: 'dd-mm-yy',
    yearRange: '2005:2020', 
    buttonImage: base_url+"images/calendar.gif",
    buttonImageOnly: true,
    showButtonPanel: true
  });
  
  cur_date('.datepicker');
});

function cur_date(class_name){
 	$(class_name).dblclick( function (){
		var currentTime = new Date();
		var day = currentTime.getDate();
		
		var month = currentTime.getMonth() + 1;
		var year = currentTime.getFullYear();
		if(day < 10){day = '0'+day;}
		if(month < 10){month = '0'+month;}
		var datStartDate = day + "-" + month + "-" + year; 
		$(this).val(datStartDate);
		});
}


function set_dwl(file,info,url){
  $("#hd_file").val(file);
  $("#hd_info").val(info);
  $("#frm_dwl").attr("action", url);
  if(url != ''){
    $("#frm_dwl").submit();
  }
}

function add_fav_jur(jur_id){  
  ajx_container 	= 'admin_msg';
	ajx_url 		= site_url+'/journals/ajax/jfav';	
	ajx_formdata 	= '';
  ajx_formdata += "aid="+jur_id;
  ajx_data_type = 'json';
  call_ajax(); 
  
}

function alert_fav_jur(art_id){

  ajx_container 	= 'admin_msg';
	ajx_url 		= site_url+'/journals/ajax/jalerts';	
	ajx_formdata 	= '';
  ajx_formdata += "aid="+art_id;
  ajx_data_type = 'json';
  call_ajax(); 
}

function fn_adda(id){
  ajx_container = 'admin_msg';
	ajx_url     	= site_url+'/cart/ajax/adda';	
	ajx_formdata 	= "id="+id;
  ajx_data_type = 'json';
  call_ajax(); 
  fn_up_shop();
  return false;
}

function fn_dela(id){
  ajx_container = 'admin_msg';
	ajx_url     	= site_url+'/cart/ajax/dela';	
	ajx_formdata 	= "id="+id;
  ajx_data_type = 'json';
  call_ajax();
  return false;
}

function fn_up_shop(){
  ajx_container = 'cart_qty';
	ajx_url     	= site_url+'/cart/ajax/shopcart';	
	ajx_formdata 	= "";
  ajx_data_type = 'update';
  call_ajax(); 
  return false;
}

/* Open new window
 * 
 * Example: window.open("URL","toolbar=no, width=350, height=400, status=no,scrollbars=yes, resize=yes, menubar=yes"); 
 */
function os_pop_up(url,set_width,set_height){
	var height 		= (set_height) ? set_height : 700;
	var width 		= (set_width) ? set_width : 800;
	var win_stats	= "toolbar=no, width="+width+", height="+height+", status=no,scrollbars=yes, resize=yes, menubar=no"
	if (navigator.appName.indexOf("Microsoft")>=0){
		win_stats+=',left=0,top=0';
	}else{	
		win_stats+=',screenX=0,screenY=-0';
	}
	var win_opened;
	if(url != ""){
		var win_opened = window.open(url,"new_window",win_stats);
		if(win_opened){
			win_opened.focus();
		}else{
			win_opened = false;
		}		
	}
	return win_opened;
}

function os_popup_blocked(url)
{
 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
 var popUpsBlocked;
 if(mine){
   popUpsBlocked = false
	 mine.close()
 }else{
    popUpsBlocked = true
 }
}

/* Window Close */
function os_window_close(check)
{
	window.close();
	if (window.opener && !window.opener.closed) {
		if(check == true){
			window.opener.location.reload();
		}
	} 	
}